package com.qimu.saiyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qimu.saiyou.common.ErrorCode;
import com.qimu.saiyou.exception.BusinessException;
import com.qimu.saiyou.mapper.CommentLikeMapper;
import com.qimu.saiyou.model.entity.Comment;
import com.qimu.saiyou.model.entity.CommentLike;
import com.qimu.saiyou.model.entity.User;
import com.qimu.saiyou.service.CommentLikeService;
import com.qimu.saiyou.service.CommentService;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author: FeiXiang
 * @description 针对表【comment_like(评论点赞表)】的数据库操作Service实现
 * @createDate 2024-03-28 16:03:50
 */
@Service
public class CommentLikeServiceImpl extends ServiceImpl<CommentLikeMapper, CommentLike>
        implements CommentLikeService {
    @Resource
    private CommentService commentService;

    /**
     * 点赞
     *
     * @param postId
     * @param postId
     * @param loginUser
     * @return
     */
    @Override
    public int doCommentLike(long commentId, Long postId, User loginUser, HttpServletRequest request) {
        // 判断实体是否存在，根据类别获取实体
        Comment comment = commentService.getById(commentId);
        if (comment == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        // 是否已点赞
        long userId = loginUser.getId();
        // 每个用户串行点赞
        // 锁必须要包裹住事务方法
        CommentLikeService commentLikeService = (CommentLikeService) AopContext.currentProxy();
        synchronized (String.valueOf(userId).intern()) {
            return commentLikeService.doCommentLikeInner(userId, commentId, postId, request);
        }
    }

    /**
     * 封装了事务的方法
     *
     * @param userId
     * @param commentId
     * @param postId
     * @param request
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int doCommentLikeInner(long userId, long commentId, Long postId, HttpServletRequest request) {
        CommentLike commentLike = new CommentLike();
        commentLike.setUserId(userId);
        commentLike.setPostId(postId);
        commentLike.setCommentId(commentId);
        QueryWrapper<CommentLike> thumbQueryWrapper = new QueryWrapper<>(commentLike);
        CommentLike oldCommentLike = this.getOne(thumbQueryWrapper);
        boolean result;
        // 已点赞
        if (oldCommentLike != null) {
            result = this.remove(thumbQueryWrapper);
            if (result) {
                // 点赞数 - 1
                result = commentService.update()
                        .eq("id", commentId)
                        .gt("likes", 0)
                        .setSql("likes = likes - 1")
                        .update();
                return result ? -1 : 0;
            } else {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR);
            }
        } else {
            // 未点赞
            result = this.save(commentLike);
            if (result) {
                // 点赞数 + 1
                result = commentService.update()
                        .eq("id", commentId)
                        .setSql("likes = likes + 1")
                        .update();
                return result ? 1 : 0;
            } else {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR);
            }
        }
    }
}




